package com.browan.freeppmobile.jni;

import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.browan.freeppmobile.jni.FreePPJNI.StatusListener;

public class FreeppKit  implements StatusListener{
	private static final String TAG = "FreeppKit";
	
	private static final String RS_HOST = "210.14.78.211:4569";
	
	private static FreeppKit instance = null;
	private static FreePPJNI kit = null;
	private Context m_context = null;
	private SharedPreferences m_sp = null;
	private int callNumber = 0;
	private int regNumber = 0;
	
	public static String freeppId = null;
	public static String e164Number = null;
	
	public static boolean needRingback = false;
	public static boolean inMeeting = false;
	
	public HashMap<String, Boolean> meetingMembers = null;
	
	static {
		System.loadLibrary("stlport_shared");
		System.loadLibrary("VoiceEngineJni");
	}
	
	public void loadAccount() {
		freeppId = m_sp.getString("freeppid", null);
		e164Number = m_sp.getString("number", null);
	}
	
	public void saveAccount() {
		Editor editor = m_sp.edit();
		editor.putString("freeppid", freeppId);
		editor.putString("number", e164Number);
		editor.commit();
	}
	
	public static FreeppKit getInstance() {
		if(instance == null)
			instance = new FreeppKit();
		
		return instance;
	}
	
	private FreeppKit()
	{
		kit = new FreePPJNI();
		kit.initialize(this);
		
		meetingMembers = new HashMap<String, Boolean>();
	}
	
	public void setContext(Context c) {
		m_sp = c.getSharedPreferences("setting", 0);
		m_context = c;
	}
	
	public void dial(String calleeId) {
		
		String user = freeppId + ":" + "11111111";
//		String callId = getFreeppId() + "-" + System.currentTimeMillis();
//		String cmd = String.format("[mobile@%s]%s", callId, number);
		
		callNumber = kit.dail(calleeId, RS_HOST, user, null);
	}
	
	public void register()
	{
		regNumber = kit.register(e164Number, freeppId, "11111111", RS_HOST);
	}
	
	public void unregister()
	{
		kit.unregister(regNumber);
	}
	
	public void answer() {
		kit.answer(callNumber);
	}
	
	public void hangup() {
		if(kit != null)
			kit.hangup(callNumber);
	}
	
	

	@Override
	public void onEvent(int type, String jsonString) {
		Log.i(TAG, "Type: "+type);
		Log.i(TAG, jsonString);
		
		jsonString = jsonString.replace("[", "^");
		jsonString = jsonString.replace("]", "*");
		try
		{
			JSONObject obj = new JSONObject(jsonString);
			switch (type)
			{
			case 0: // EVENT_REGISTRATION
				Log.i(TAG, "Iax Registered");
				break;
			case 1: // EVENT_STATE
				int curState = obj.getInt("activity");

				Intent intent = new Intent();

				switch (curState)
				{
				case 0: // CALL_FREE
					Log.i(TAG, "Iax hangup");
					intent.setAction("iax.hangup");
					break;
				case 2: // CALL_RINGIN
					Log.i(TAG, "Iax ringin");
					callNumber = obj.getInt("callNo");
//					String cmd = obj.getString("peername");
//
//					int idx1 = cmd.indexOf("@");
//					int idx2 = cmd.indexOf("*");
//					String callId = cmd.substring(idx1 + 1, idx2);
					
					intent.setAction("iax.new.call");
					break;
				case 1: // CALL_OUTGOING
				case 3: // CALL_RINGBACK
					// do nothing
					break;
				case 4: // CALL_ANSWERED
					Log.i(TAG, "Iax Answered");
					break;
				}
				
				m_context.sendBroadcast(intent);
				break;
			}
		}
		catch (JSONException e)
		{
			// Print.e(TAG, "Freepp Engine Callback Error");
		}
	}
}
